package vn.demand.dragon.view
{
	import com.adobe.utils.NumberFormatter;
	
	import flash.events.Event;
	
	import flexed.widgets.alerts.alert;
	
	import mx.controls.Alert;
	
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	import vn.demand.dragon.CommonCommand;
	import vn.demand.dragon.FFIAApplicationFacade;
	import vn.demand.dragon.model.MasterdataProxy;
	import vn.demand.dragon.model.ReferenceProxy;
	import vn.demand.dragon.view.components.ProjectNavigator;
	import vn.demand.utils.DpUtils;
	
	public class ProjectNavigatorMediator extends Mediator
	{
		
		public static const NAME:String = 'ProjectNavigatorMediator';
		
		private var masterdataProxy:MasterdataProxy = null;
		private var projectProxy:ReferenceProxy = null;
		
		public static function makeId(projectVO):String {
			return NAME + "::" + projectVO.project_iso_code;
		}
		
		public var projectVO:Object;
		
		public var caller:String;
		
		public function ProjectNavigatorMediator(mediatorId:String, view:Object, projectVO, caller)
		{
			super(mediatorId, view);
			
			this.projectVO = projectVO;
			this.caller = caller;
			
			masterdataProxy = facade.retrieveProxy(MasterdataProxy.NAME) as MasterdataProxy;
			projectProxy = facade.retrieveProxy(ReferenceProxy.NAME) as ReferenceProxy;
			
			navigator.addEventListener('viewCreated', prepareData); 
			navigator.addEventListener('onLoad', onLoadReference); 
		}
		
		public function prepareData(e:Event):void {
			navigator.company_code.dataProvider = masterdataProxy.customersDP;
			navigator.company_code.labelField = 'company_code';
			navigator.company_code.selectedData = projectVO.project_company;
			
			navigator.shipping_type_code.dataProvider = masterdataProxy.shippingTypesDP;
			navigator.shipping_type_code.labelField = 'code';
			navigator.shipping_type_code.selectedData = projectVO.project_shipping_type;
			
			navigator.reference_date.text = DpUtils.dateFormatter.format(DpUtils.parseDate(projectVO.project_date));
        	navigator.start_date.text = DpUtils.dateFormatter.format(DpUtils.parseDate(projectVO.project_start_date));
        	
        	setProjectCodeInfo();
		}
		
		public function setProjectCodeInfo() {
			var pattern:RegExp = /(\d+)/g;
        	var arr:Array = String(projectVO.project_iso_code).match(pattern);
        	navigator.month.value = Number(arr[0]);
        	navigator.year.value = Number(arr[1]);
        	navigator.number.value = Number(arr[2]);
        	
        	navigator.iso_code.text = projectVO.project_iso_code;
		}
		
		public function onLoadReference(e:Event):void {
			var item:Object = navigator.company_code.selectedItem;
			var projectCode = item.company_code + '-';
			item = navigator.shipping_type_code.selectedItem;
			projectCode += item.code + '-';
			projectCode += NumberFormatter.addLeadingZero(Number(navigator.month.value ))+ '-' + navigator.year.value + '-' + navigator.number.value;
			sendNotification(CommonCommand.BEFORE_LOAD_REFERENCE, projectCode, caller);
		}
		
		public function doLoadReference():void {
			var item:Object = navigator.company_code.selectedItem;
			var projectCode = item.company_code + '-';
			item = navigator.shipping_type_code.selectedItem;
			projectCode += item.code + '-';
			projectCode += NumberFormatter.addLeadingZero(Number(navigator.month.value ))+ '-' + navigator.year.value + '-' + navigator.number.value;
			projectProxy.loadProjectByCode(projectCode, caller);
		}
		
		public function get navigator():ProjectNavigator {
			return viewComponent as ProjectNavigator;
		}
		
		override public function listNotificationInterests():Array 
        {
            return [ 
            	FFIAApplicationFacade.LOAD_REFERENCE_DETAIL_SUCCESS,
            	FFIAApplicationFacade.LOAD_REFERENCE_DETAIL_FAILED,
            	CommonCommand.OK_FOR_LOAD_REFERENCE,
            	CommonCommand.CANCEL_FOR_LOAD_REFERENCE
					 ];
        }
        
        override public function handleNotification( note:INotification ):void 
        {
        	switch (note.getName()) {
        		case CommonCommand.OK_FOR_LOAD_REFERENCE:
        			if (note.getType() != caller) {
        				return;
        			}
        			doLoadReference();
        			break;
        		case FFIAApplicationFacade.LOAD_REFERENCE_DETAIL_SUCCESS:
        			if (note.getType() == caller) {
        				projectVO = note.getBody() as Object;
        				navigator.iso_code.text = projectVO.project_iso_code;
        				navigator.reference_date.text = DpUtils.dateFormatter.format(DpUtils.parseDate(projectVO.project_date));
        				navigator.start_date.text = DpUtils.dateFormatter.format(DpUtils.parseDate(projectVO.project_start_date));
        			}
        			break;
        		case FFIAApplicationFacade.LOAD_REFERENCE_DETAIL_FAILED:
        			if (note.getType() == caller) {
	        			var project = note.getBody() as Object;
						alert.error('Reference code ' + project.project_iso_code + ' not found');
						navigator.reference_date.text = '';
        				navigator.start_date.text = '';
        				navigator.iso_code.text = '';
        			}
        			break;
        		case CommonCommand.CANCEL_FOR_LOAD_REFERENCE:
        			if (note.getType() == caller) {
        				setProjectCodeInfo();
        			}
        			break;
        	}
        }

	}
}